package org.app.Controller.Admin;

import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.app.Entity.Admin;
import org.app.Entity.comm.Result;
import org.app.Service.IAdminService;
import org.app.Utils.SMSUtil;
import org.app.Utils.ValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin")
@CrossOrigin("*")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public Result<Admin> login(@RequestParam("admin_phone") String admin_phone,
                               @RequestParam("admin_password") String admin_password){
        return adminService.adminLogin(admin_phone,admin_password);
    }
    @PostMapping("/register")
    public Result<Integer> Register(@RequestBody Admin admin){
        return adminService.adminRegister(admin);
    }
    @PutMapping("/updateInfo")
    public Result<Integer> updateAdminInfo(@RequestBody Admin admin){
        return adminService.updateAdminInfo(admin);
    }

    @PutMapping("/updatePassword")
    public Result<Integer> updateUserPassword(@RequestParam("admin_id") String admin_id,
                                              @RequestParam("new_password") String new_password,
                                              @RequestParam("old_password") String old_password){

        return adminService.updateAdminPassword(admin_id,new_password,old_password);
    }
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestParam String admin_phone){
        Result<String> result = new Result<>();
        if(adminService.getAdminByPhone(admin_phone) == null){
            result.setCode(-1);
            result.setMessage("该手机号未被注册");
            result.setData(null);
            return result;
        }else {
            if (StringUtils.isNotEmpty(admin_phone)){
//            Long phone = Long.parseLong(c_phone);
                // 生成随机4位验证码
                String code = ValidateCodeUtil.generateValidateCode(4).toString();
//            log.info("code={}",code);
                // 调用阿里云提供的短信服务API完成发送短信
                SMSUtil.sendMessage("运动锻炼APP","SMS_473430042" ,admin_phone,code);

                // 将生成的验证码缓存到Redis种，设置有效期5分钟
                redisTemplate.opsForValue().set(admin_phone,code,5, TimeUnit.MINUTES);

                // 没有redis 将生成的验证码保存到Session
                // session.setAttribute(phone,code);

                result.setCode(200);
                result.setMessage("发送成功");
                result.setData(code);
            }else {
                result.setCode(500);
                result.setMessage("发送失败");
                result.setData(null);
            }
        }
        return result;
    }

    @PostMapping("/forgetPassword")
    public Result<Integer> forgetPassword(@RequestBody Map map){
        // 获取手机号
        String admin_phone = map.get("admin_phone").toString();
        // 获取验证码
        String code = map.get("code").toString();
        String admin_id = map.get("admin_id").toString();
        String new_password = map.get("new_password").toString();
        String codeInSession = redisTemplate.opsForValue().get(admin_phone).toString();

        // 进行验证码的比对
        if (codeInSession!=null && codeInSession.equals(code)){
            // 如果比对成功，则继续提供服务
            // 对比成功 删除缓存验证码
            redisTemplate.delete(admin_phone);
            return adminService.forgetPassword(admin_id,new_password);
        } else {
            Result<Integer> result = new Result<>();
            result.setCode(500);
            result.setMessage("验证码错误");
            result.setData(0);
            return result;
        }
    }
}